package com.lc.admin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtils {

    // 传递一个日期，只取出年月日部分   1 - 12
    public static String getDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR) + "-" + padding(calendar.get(Calendar.MONTH) +1) + "-" + padding(calendar.get(Calendar.DAY_OF_MONTH));
    }

    public static String padding(int number) {
        if(number<10) {
            return "0"+number;
        }
        return number+"";
    }

    // 得到是上午还是下午
    public static String getAmPm(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 获取小时（24小时制）

        if (hourOfDay < 12) {
            return "am";
        } else {
            return "pm";
        }
    }

    // 把字符串转换成日期
    public static Date parse(String date)  {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {

    }
}
